<?php

require_once( '../system/smarty.inc' );

require_once( '../system/tdao/Class_B_HISTORY.php' );
require_once( '../system/mdao/Class_M_OTHER.php' );

require_once( '../system/Class_ERROR.php' );
require_once( '../system/login.inc.php' );
require_once( '../system/aoki_m.php' );
require_once( '../system/aoki_rhizome.php' );
require_once( '../system/Class_PWD.php' );

Class_PWD::valid_auth(AOKI_AUTH_SHOP,$login['staff_auth']);

$dosearch = FALSE ;
$dao = new Class_B_HISTORY;

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ||
	 isset( $_GET['hist'] ) &&  (  $_GET['hist'] == "2" ) ) {
	$forms =  $_SESSION['MSEARCH']['RHIZOME'];
}else {
	$forms = $_GET;
	$_SESSION['MSEARCH']['RHIZOME'] = $forms;
}

$wherearr =  array();
if ( isset( $forms['sort'] ) &&  ( !empty( $forms['sort'] ) ) ){
	$wherearr[] = sprintf(" sort =  '%s' ",  $dao->esc( $forms['sort'] ) );
}

if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "import" ) ){
		$forms["year4"]    = $_GET['year4'];
		$forms["matrix_cd"]  = $_GET['matrix_cd'];
	
}

$users = Class_M_OTHER::get_user_info();
/*

//---CSVダウンロード　処理開始
if ( isset( $_GET['csv'] ) &&  (  $_GET['csv'] == "download" ) ){
	$_GET["csv"] = "";
	$tmpfilename = "shop".date("Ymd_His");
	header("Content-Type: application/octet-stream"); 
	header("Content-Disposition: attachment; filename=$tmpfilename.csv");
	$data = $dao->searchcsv( $wherearr );
//列名
	$colums = array();

//1 string code 2 integer 3 date 4 string ""必要 
	$col_heads = array(
		'shop_cd'        => array( '店舗コード',      1),
		'shop_name'      => array( '店舗名',          1),
		'parent_shop_cd' => array( '親店舗コード',    1),
		'p_shop_name'    => array( '親店舗名',        1),
		'block_cd'     => array( '地域ブロックコード',1),
		'block_name'   => array( '地域ブロック名',    1),
		'local_cd'     => array( '都道府県コード',    1),
		'local_name'   => array( '都道府県名',        1),
		'postcode'     => array( '郵便番号',          1),
		'address'      => array( '住所',              1),
		'tel'          => array( '電話番号',          1),
//		'smx'          => array( 'SMXフラグ',         1),
//		'smx_name'     => array( 'SMXフラグ',         1),
		'center'       => array( '都心フラグ',        1),
		'center_name'  => array( '都心フラグ',        1),
		'lnews1'       => array( '地域新聞名１コード',1),
		'lnews1_name'  => array( '地域新聞名１名称',  1),
		'lnews2'       => array( '地域新聞名３コード',1),
		'lnews2_name'  => array( '地域新聞名２名称',  1),
		'lnews3'       => array( '地域新聞名３コード',1),
		'lnews3_name'  => array( '地域新聞名３名称',  1),
		'lnews4'       => array( '地域新聞名４コード',1),
		'lnews4_name'  => array( '地域新聞名４名称',  1),
		'lnews5'       => array( '地域新聞名５コード',1),
		'lnews5_name'  => array( '地域新聞名５名称',  1),
		'stop_date'    => array( '廃止日',            3),
		'u_time'       => array( '更新日時',          3),
		'c_time'       => array( '登録日時',          3),
		'm_user_name'  => array( '更新者',            1)
	);

	//列名
	foreach ($col_heads as $k=>$v) {
		$colums["name"][]   = $k;
		$colums["head"][]   = $v[0];
		$colums["type"][$k] = $v[1];
	}
	//データ整形開始
	foreach ($data as $k => $v) {
		$data[$k]["center_name"] =  $aoki_set_flags[$v["center"]];
	//	$data[$k]["smx_name"]    =  $aoki_set_flags[$v["smx"]];
		$data[$k]["stop_date"]   =  Class_PWD::int8_to_date($v["stop_date"]);
		$data[$k]["c_time"]      =  Class_PWD::dateTime_to_YmdHi($v["c_time"]);
		$data[$k]["u_time"]      =  Class_PWD::dateTime_to_YmdHi($v["u_time"]);
	}
	//データ整形完了
	$dao->output_csv($data, $colums);
	exit;
}

//---CSVダウンロード　処理完了
*/

if ( !$forms['sort'] ) {
	$forms['sort'] = "1";
}
if ( isset( $forms['search'] ) && (  !empty( $forms['search'] ) ) ){
	$dosearch = TRUE;
}

if( isset( $forms['page'] ) && is_numeric( $forms['page'] ) ){
	$dosearch = TRUE;
	$page['current'] = $forms['page'];	
}else{
	$page['current'] = 1;
}
$page['item'] = PAGE_MAX;
if ($dosearch) {
	//$page内容は関数内部更新されます。
	$data = $dao->search( $wherearr, $page );
	foreach ($data as $k=>$v) {
		if ( isset($users[$v["u_user"]]) ){
			$data[$k]["u_user"]= $users[$v["u_user"]]["user_name"];
		}else {
			$data[$k]["u_user"] = "システム";
		}
	}
}

$smarty->assign('import', $smarty->fetch('import.html') );

if (empty($forms["year4"])) {
	$forms["year4"] = date('Y');
}

$smarty->assign('year4',  Class_PWD::get_years());
$week= Class_M_OTHER::get_week_array( $forms["year4"], PLEASE_SELECT_ARR );
$smarty->assign('week', $week );


if( isset($forms) ) $smarty->assign('forms', $forms);
if( isset($data) )  $smarty->assign('data', $data);
if( isset($page) )  $smarty->assign('page', $page);

$smarty->assign('sorts', $aoki_rhizome_sorts );
$smarty->assign('rests', $aoki_rhizome_results );

$smarty->assign('menu',  $smarty->fetch('menu.html') );
$smarty->assign('logout', $smarty->fetch('logout.html') );
$smarty->assign('footer', $smarty->fetch('footer.html') );
$smarty->assign('pageing', $smarty->fetch('page.html') );


$smarty->display('mrhizome_index.html');



?>